var path = require('path');
var webpack = require('webpack');
var srcDir = path.resolve(process.cwd(), 'src');
var imagePath = path.resolve(srcDir, 'img');
var cssPath = path.resolve(srcDir, 'css');
var jsPath = path.resolve(srcDir, 'js');
var viewsPath = path.resolve(srcDir, 'views');
var nodeModulesPath = path.resolve(__dirname, 'node_modules');
var autoprefixer = require('autoprefixer');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var pxtorem = require('postcss-pxtorem');
var bundleConfig = require("./build/bundle-config.json");
var CleanPlugin = require('clean-webpack-plugin');
var buildDir = path.join(process.cwd(), 'build');
outputbuildDir = path.join(buildDir, 'static');
outputbuildDir = path.join('D:/code/skycount-app-android/www');
outputbuildDir = path.join('D:/dev/AppCanStudioPersonalV4.0/widgetapp/我店-11699383/phone');

let webpackConfig = {
	entry: {
		index: ['babel-polyfill', path.join(__dirname, "src/views/index.jsx")]
	},
	output: {
		publicPath: './',
		path: path.join(outputbuildDir, "/"), //文件输出目录
		filename: "core/[name].[hash:5].js", //根据入口文件输出的对应多个文件名
		chunkFilename: "core/[name].[chunkhash:5].js"
	},
	resolve: {
		extensions: ['.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx', '.js', '.jsx', '.react.js', '.json'],
		alias: { //配置别名，在项目中可缩减引用路径
			'img': path.join(srcDir, 'img'),
			'css': path.join(srcDir, 'css'),
			'js': path.join(srcDir, 'js'),
			'views': path.join(srcDir, 'views')
		}
	},
	module: {
		rules: [{
			test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif|mp4|webm)(\?\S*)?$/,
			exclude: [nodeModulesPath],
			include: [srcDir],
			loader: 'url-loader?limit=200&name=img/[name].[hash:5].[ext]',
		},
		{
			test: /\.json$/,
			loader: 'json-loader'
		},
		{
		    test: /\.(css|scss|sass)$/,
		    use: ExtractTextPlugin.extract({
		        use: [{
		                loader: 'css-loader',
		            },
		            {
		                loader: 'postcss-loader',
		                options: {
		                    plugins: [autoprefixer, pxtorem({
		                        rootValue: 50,
		                        minPixelValue: 3,
		                        propWhiteList: [],
		                    })]
		                }
		            }, {
		                loader: "sass-loader",
		                options: {
		                    imagePath: imagePath,
		                    includePaths: [cssPath]
		                }
		            }
		        ],
		        fallback: 'style-loader'
		    })
		},
		// {
		// 	test: /\.(css|scss|sass)$/,
		// 	use: ["style-loader", {
		// 		loader: 'css-loader',
		// 	},
		// 		{
		// 			loader: 'postcss-loader',
		// 			options: {
		// 				plugins: [autoprefixer, pxtorem({
		// 					rootValue: 50,
		// 					minPixelValue: 3,
		// 					propWhiteList: [],
		// 				})]
		// 			}
		// 		}, {
		// 			loader: "sass-loader",
		// 			options: {
		// 				imagePath: imagePath,
		// 				includePaths: [cssPath]
		// 			}
		// 		}
		// 	],
		// },
		{
			test: /\.svg$/,
			loader: 'svg-sprite-loader',
			include: [require.resolve('antd-mobile').replace(/warn\.js$/, '')],
		},
		{
			test: /\.(js|jsx)$/,
			exclude: [nodeModulesPath],
			include: [srcDir],
			loader: "babel-loader"
		}
		]
	},
	plugins: [
		new webpack.DefinePlugin({
			'process.env.NODE_ENV': '"production"'
		}),
		new webpack.DllReferencePlugin({ // 加快webpack打包速度
			context: __dirname,
			manifest: require('./build/static/dll/vendor-manifest.json')
		}),
		new CopyWebpackPlugin([{
			from: path.resolve(__dirname, './src/js/lib'),
			to: path.resolve(outputbuildDir, './lib')
		}]),
		new ExtractTextPlugin('css/[name].[contenthash:5].css'),
		// new webpack.optimize.UglifyJsPlugin({
		//     output: {
		//         comments: false,
		//     },
		//     compress: {
		//         warnings: false
		//     }
		// }),
		new HtmlWebpackPlugin({
			filename: path.resolve(outputbuildDir, './index.html'),
			template: path.resolve(__dirname, './src/html/index.html'),
			bundleName: '.' + bundleConfig.vendor.js,
			inject: true
		})
	]
};


webpackConfig.plugins.push(new CleanPlugin(['core'], {
	root: outputbuildDir,
	verbose: true,
	dry: false
}));
webpackConfig.plugins.push(new CleanPlugin(['img'], {
	root: outputbuildDir,
	verbose: true,
	dry: false
}));
webpackConfig.plugins.push(new CleanPlugin(['lib'], {
	root: outputbuildDir,
	verbose: true,
	dry: false
}));
webpackConfig.plugins.push(new CleanPlugin(['css'], {
	root: outputbuildDir,
	verbose: true,
	dry: false
}));


module.exports = webpackConfig;
